$(function () {
    var carts = [];
    var size = 0;
    var num = 0;
    var productNum = 0;

    /**
     * 获取购物车列表
     */
    $.ajax({
        type: 'GET',
        url: ajaxUrl("queryCart"),
        success: function (result) {
            if (result.code === 400) {
                localStorage.setItem('returnUrl', location.href);
                location.href = "login.html";
                return;
            }
            carts = result.data;
            // console.log(result);

            $('#cartBox').html(template('cartTpl', {
                data: result.data
            }))
        }
    });

    /**
     * 删除购物车
     */
    $('body').on('tap', '.deleteBtn', function () {
        var id = $(this).attr('data-id');

        var btnArray = ['取消', '确定'];
        mui.confirm("确定要删除该商品吗", '删除商品', btnArray, function (e) {
            if (e.index == 1) {
                $.ajax({
                    type: 'GET',
                    url: ajaxUrl("deleteCart"),
                    data: {
                        id: [id]
                    },
                    success: function (result) {
                        if (result.code === 400) {
                            localStorage.setItem('returnUrl', location.href);
                            location.href = "login.html";
                            return;
                        }
                        if (result.code === 200) location.reload();
                    }
                });
            } else {
                mui.toast('点击取消', {
                    duration: 'short',
                    type: 'div'
                })
            }
        });
    });

    /**
     * 编辑购物车
     */
    $('body').on('tap', '.editBtn', function () {
        var id = $(this).attr('data-id');
        var item = findGoodsById(carts, id);
        productNum = item.productNum;
        num = item.num;
        size = item.size;
        var productSize = item.productSize.split('-');
        var start = parseInt(productSize[0]);
        var end = parseInt(productSize[1]);

        item.customSize = [];

        for (var i = start; i <= end; i++) {
            item.customSize.push(i);
        }

        var html = template('detailTpl', {
            data: item
        }).replace(/\n/g, '');

        var btnArray = ['取消', '确定'];

        mui.confirm(html, '编辑商品', btnArray, function (e) {
            if (e.index == 1) {
                $.ajax({
                    type: 'POST',
                    url: ajaxUrl("updateCart"),
                    data: {
                        id: id,
                        size: size,
                        num: num
                    },
                    success: function (result) {
                        if (result.code === 200) {
                            alert('修改成功');
                            location.reload();
                        }
                    }
                });
            } else {
                mui.toast('点击取消', {
                    duration: 'short',
                    type: 'div'
                })
            }
        });
    });

    /**
     * 更改尺码
     */
    $('body').on('tap', '.detail-size span', function () {
        $(this).addClass('active').siblings().removeClass('active');
        size = $(this).html();
    });

    /**
     * 减少数量
     */
    $('body').on('tap', '.detail-num .reduce', function () {
        num = $('.detail-num .num').val();
        num--;
        if (num < 0) {
            num = 0;
        }
        $('.detail-num input').val(num);
    })

    /**
     * 增加数量
     */
    $('body').on('tap', '.detail-num .plus', function () {
        num = $('.detail-num .num').val();
        num++;
        if (num > productNum) {
            num = productNum;
        }
        $('.detail-num input').val(num);
    })

});

function findGoodsById(result, id) {
    console.log(result)
    for (var i = 0; i < result.length; i++) {
        if (result[i].id == id) {
            return result[i];
        }
    }
}